{template 'common/header'}
<style>
    .had_gen_pay{
        color:green;
    }
    .no_gen_pay{
        color:red;
    }
</style>
<script>
    order_ids = new Array();
    i = 0;
    $(function(){
        $("#se_member").on('blur',function(){
            $(".find").click();
        });
        $("#order_member").on('blur',function(){
            $(".find").click();
        });
        $("#se_goods").on('blur',function(){
            $(".find").click();
        });
        $("#type").on("change",function(){
            $(".find").click();
        });
        $("#order_day_start").on('change',function(){
            $(".find").click();
        });
        $("#order_day_end").on('change',function(){
            $(".find").click();
        });
        $("#se_day_start").on('change',function(){
            $(".find").click();
        });
        $("#se_day_end").on('change',function(){
            $(".find").click();
        });
        $("#member-region").on('change',function(){
            $(".find").click();
        });
        $("#from-region").on('change',function(){
            $(".find").click();
        });
        $("#se_man").on('change',function(){
            $(".find").click();
        });
        $("#se_remark").on('blur',function(){
            $(".find").click();
        });
        $("#gen_pay").on('blur',function(){
            $(".find").click();
        });
        $(".se_remark").blur(function(){
            var se_remark = $(this).val();
            var url = "{php echo $this->createWebUrl('orders',array('op' => 'change_order_goods'))}";
            var order_goods_id = $(this).parent().parent().attr('order_goods_id');
            $.ajax({
                type : "post",
                url : url,
                data : {'se_remark' : se_remark, 'order_goods_id' : order_goods_id},
                success : function(data){
                    if(data == 111){
                        location.reload();
                    }else{
                        alert(data);
                    }
                }
            });
        });
        $(".pay").blur(function(){
            var pay = $(this).val();
            if(confirm("是否确定结算金额"+pay+"?")) {
                var url = "{php echo $this->createWebUrl('orders',array('op' => 'change_order_goods'))}";
                var order_goods_id = $(this).parent().parent().attr('order_goods_id');
                $.ajax({
                    type: "post",
                    url: url,
                    data: {'pay': pay, 'order_goods_id': order_goods_id},
                    success: function (data) {
                        if (data == 111) {
                            location.reload();
                        } else {
                            alert(data);
                        }
                    }
                });
            }
        });
        $(".gen-pay").blur(function(){
            var pay = $(this).val();
            if(confirm("是否将已收代付款修改为"+pay+"?")) {
                var url = "{php echo $this->createWebUrl('orders',array('op' => 'change_order_goods'))}";
                var order_goods_id = $(this).parent().parent().attr('order_goods_id');
                $.ajax({
                    type: "post",
                    url: url,
                    data: {'had_gen_pay': pay, 'order_goods_id': order_goods_id},
                    success: function (data) {
                        if (data == 111) {
                            location.reload();
                        } else {
                            alert(data);
                        }
                    }
                });
            }
        });
        $(".had_charge").blur(function(){
            var pay = $(this).val();
            if(confirm("是否确定将已收运费修改为"+pay+"？")) {
                var url = "{php echo $this->createWebUrl('orders',array('op' => 'change_order_goods'))}";
                var order_goods_id = $(this).parent().parent().attr('order_goods_id');
                $.ajax({
                    type: "post",
                    url: url,
                    data: {'had_charge': pay, 'order_goods_id': order_goods_id},
                    success: function (data) {
                        if (data == 111) {
                            location.reload();
                        } else {
                            alert(data);
                        }
                    }
                });
            }
        });
        $(".find").click(function(){
            var se_member = $("#se_member").val();
            var order_member = $("#order_member").val();
            var se_goods = $("#se_goods").val();
            var type = $("#type").val();
            var order_day_start = $("#order_day_start").val();
            var order_day_end = $("#order_day_end").val();
            var se_day_start = $("#se_day_start").val();
            var se_day_end = $("#se_day_end").val();
//            var member_region = $("#member-region").val();
//            var from_region = $("#from-region").val();
            var se_man = $("#se_man").val();
            var se_remark = $("#se_remark").val();
            var gen_pay = $("#gen_pay").val();
            location.href="{php echo $this->createWebUrl('settlement')}&se_member="+se_member+"&order_member="+order_member+"&se_goods="+se_goods+"&type="+type+"&order_day_start="+order_day_start+"&order_day_end="+order_day_end+"&se_day_start="+se_day_start+"&se_day_end="+se_day_end+"&se_man="+se_man+"&se_remark="+se_remark+"&gen_pay="+gen_pay;
        });
        $(".export-data").click(function(){
            var se_member = $("#se_member").val();
            var order_member = $("#order_member").val();
            var se_goods = $("#se_goods").val();
            var type = $("#type").val();
            var order_day_start = $("#order_day_start").val();
            var order_day_end = $("#order_day_end").val();
            var se_day_start = $("#se_day_start").val();
            var se_day_end = $("#se_day_end").val();
            var member_region = $("#member-region").val();
            var from_region = $("#from-region").val();
            var se_man = $("#se_man").val();
            var se_remark = $("#se_remark").val();
            var gen_pay = $("#gen_pay").val();
            location.href="{php echo $this->createWebUrl('settlement',array('op' => 'export'))}&se_member="+se_member+"&order_member="+order_member+"&se_goods="+se_goods+"&type="+type+"&order_day_start="+order_day_start+"&order_day_end"+order_day_end+"&se_day_start="+se_day_start+"&se_day_end"+se_day_end+"&se_man="+se_man+"&se_remark="+se_remark+"&gen_pay="+gen_pay+"&member_region="+member_region+"&from_region="+from_region;
        });
        $(".save_data").on('click',function(){
            var id = $(this).attr('order_id');
            var had_charge = parseFloat($("#had_charge" + id).val());
            var had_se_pay = parseFloat($("#had_se_pay" + id).val());
//            var se_remark = $("#se_remark" + id).val();
            $("#had_charge" + id).attr('readonly','readonly');
            $("#had_se_pay" + id).attr('readonly','readonly');
            $("#se_remark" + id).attr('readonly','readonly');
            var total_charge = parseFloat($("#total_charge").html());
            var total_gen = parseFloat($("#total_gen").html());
            total_charge += had_charge;
            total_gen += had_se_pay;
            $("#total_charge").html(total_charge);
            $("#total_gen").html(total_gen);
            $("#total_pay").html(total_gen-total_charge);
            order_ids[i] = id;
            i++;
            $(this).hide();
            $("#cancel-btn"+id).show();
        });
        $(".cancel-data").on("click",function(){
            var id = $(this).attr('order_id');
            var had_charge = parseFloat($("#had_charge" + id).val());
            var had_se_pay = parseFloat($("#had_se_pay" + id).val());
//            var se_remark = $("#se_remark" + id).val();
            $("#had_charge" + id).removeAttr('readonly');
            $("#had_se_pay" + id).removeAttr('readonly');
            $("#se_remark" + id).removeAttr('readonly');
            var total_charge = parseFloat($("#total_charge").html());
            var total_gen = parseFloat($("#total_gen").html());
            total_charge -= had_charge;
            total_gen -= had_se_pay;
            $("#total_charge").html(total_charge);
            $("#total_gen").html(total_gen);
            $("#total_pay").html(total_gen-total_charge);
            order_ids.splice($.inArray(id,order_ids),1);
            $(this).hide();
            $("#save-btn"+id).show();
        });
        $(".save-all").on('click',function(){
            if(confirm("是否确定结算这些代付款？")) {
                for (var j = 0; j < order_ids.length; j++) {
                    save_data(order_ids[j]);
                }
                location.reload();
            }
//            var id = $(this).attr('order_id');
//            var had_charge = parseFloat($("#had_charge" + id).val());
//            var had_se_pay = parseFloat($("#had_se_pay" + id).val());
//            var se_remark = $("#se_remark" + id).val();
//            $("#had_charge" + id).attr('readonly','readonly');
//            $("#had_se_pay" + id).attr('readonly','readonly');
//            $("#se_remark" + id).attr('readonly','readonly');
//            var total_charge = parseFloat($("#total_charge").html());
//            var total_gen = parseFloat($("#total_gen").html());
//            total_charge -= had_charge;
//            total_gen -= had_se_pay;
//            $("#total_charge").html(total_charge);
//            $("#total_gen").html(total_gen);
//            $("#total_pay").html(total_gen-total_charge);
        });
    });
    function save_data(id){
//        if("是否确定结算该订单？") {
            var had_charge = $("#had_charge" + id).val();
            var had_se_pay = $("#had_se_pay" + id).val();
            var se_remark = $("#se_remark" + id).val();
            var url = "{php echo $this->createWebUrl('settlement',array('op' => 'change_se'))}";
            $.ajax({
                type : 'post',
                url : url,
                data : {'had_charge' : had_charge, 'had_se_pay' : had_se_pay, 'se_remark' : se_remark, 'id' : id},
                success : function(data){
//                    if(data == 111){
//                        location.reload();
//                    }else{
//                        alert(data);
//                    }
                }
            });
//        }
    }
</script>
<ul class="nav nav-tabs">
    <li class="active" ><a href="{php echo $this->createWebUrl('settlement', array('op'=>'index'))}">代付款结算单</a></li>
    <!--<li><a href="{php echo $this->createWebUrl('settlement', array('op'=>'collection'))}">代收款结算单</a></li>-->
</ul>
<div class="main">
    <div class="panel panel-primary">
        <div class="panel-body">
            <div class="row">
                <div class="col-xs-3">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">商户</label>
                    <div class="col-sm-9 col-xs-12">
                        <input type="text" id="se_member" class="form-control" value="{$se_member}" />
                    </div>
                </div>
                <div class="col-xs-3">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">客户</label>
                    <div class="col-sm-9 col-xs-12">
                        <input type="text" id="order_member" class="form-control" value="{$order_member}" />
                    </div>
                </div>
                <div class="col-xs-3">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">品名</label>
                    <div class="col-sm-9 col-xs-12">
                        <input type="text" id="se_goods" class="form-control" value="{$se_goods}" />
                    </div>
                </div>
                <div class="col-xs-3">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">状态</label>
                    <div class="col-sm-9 col-xs-12">
                        <select class="form-control" id="type" >
                            <option value="0">所有状态</option>
                            <option value="1" {if $type == 1} selected="selected" {/if}>未结算</option>
                            <option value="2" {if $type == 2} selected="selected" {/if}>已结算</option>
                        </select>
                    </div>
                </div>
            </div>
            <br />
            <div class="row">
                <!--<div class="col-xs-3">-->
                    <!--<label class="col-xs-12 col-sm-3 col-md-2 control-label">商户区域</label>-->
                    <!--<div class="col-sm-9 col-xs-12">-->
                        <!--<select class="form-control" id="from-region">-->
                            <!--<option value="0">请选择商户区域</option>-->
                            <!--{loop $regions $region}-->
                            <!--<option value="{$region['id']}" {if $region['id'] == $from_region} selected="selected" {/if}>{$region['name']}</option>-->
                            <!--{/loop}-->
                        <!--</select>-->
                    <!--</div>-->
                <!--</div>-->
                <!--<div class="col-xs-3">-->
                    <!--<label class="col-xs-12 col-sm-3 col-md-2 control-label">客户区域</label>-->
                    <!--<div class="col-sm-9 col-xs-12">-->
                        <!--<select class="form-control" id="member-region">-->
                            <!--<option value="0">请选择客户区域</option>-->
                            <!--{loop $regions $region}-->
                            <!--<option value="{$region['id']}" {if $region['id'] == $member_region} selected="selected" {/if} >{$region['name']}</option>-->
                            <!--{/loop}-->
                        <!--</select>-->
                    <!--</div>-->
                <!--</div>-->
                <div class="col-xs-3">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">订单日期</label>
                    <div class="col-sm-9 col-xs-12">
                        <input type="date" class="form-control" id="order_day_start" value="{$order_day_start}" />
                    </div>
                </div>
                <div class="col-xs-3">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">到</label>
                    <div class="col-sm-9 col-xs-12">
                        <input type="date" class="form-control" id="order_day_end" value="{$order_day_end}" />
                    </div>
                </div>


                <div class="col-xs-3">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">结算日期</label>
                    <div class="col-sm-9 col-xs-12">
                        <input type="date" class="form-control" id="se_day_start" value="{$se_day_start}" />
                    </div>
                </div>
                <div class="col-xs-3">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">到</label>
                    <div class="col-sm-9 col-xs-12">
                        <input type="date" class="form-control" id="se_day_end" value="{$se_day_end}" />
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-3">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">结算人</label>
                    <div class="col-sm-9 col-xs-12">
                        <select class="form-control" id="se_man">
                            <option value="0">请选择结算人</option>
                            {loop $users $user}
                            <option value="{$user['username']}" {if $user['uid'] == $se_man} selected="selected" {/if}>
                                {$user['username']}
                            </option>
                            {/loop}
                        </select>
                    </div>
                </div>
                <div class="col-xs-3">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">标注</label>
                    <div class="col-sm-9 col-xs-12">
                        <input type="text" id="se_remark" class="form-control" value="{$se_remark}" />
                    </div>
                </div>
                <div class="col-xs-3">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">代付款</label>
                    <div class="col-sm-9 col-xs-12">
                        <input type="text" id="gen_pay" class="form-control" value="{$gen_pay}" />
                    </div>
                </div>
                <div class="col-xs-3">
                    <div class="col-sm-9 col-xs-12">
                        <button class="btn btn-default find">搜索</button>
                        <button class="btn btn-default export-data">导出</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <table class="table">
        <tr>
            <td></td>
            <th>商户名</th>
            <th>订单日期</th>
            <th>客户名</th>
            <th>品名</th>
            <th>代付款</th>
            <th>手续费</th>
            <th>已收代付款</th>
            <th>手续费</th>
            <th>结算金额</th>
            <th>完成状态</th>
            <th>标注</th>
            <th>结算时间</th>
            <th>结算人</th>
            <th>备注</th>
            <th>结算</th>
        </tr>
        {loop $list $l}
        <tr order_goods_id="{$l['id']}" {if $l['had_gen_pay'] > 0} class="had_gen_pay" {else} class="no_gen_pay" {/if}>
            <td></td>
            <td>
                {if $l['company']}
                {$l['company']['name']}
                {else}
                {$l['member']['name']}
                {/if}
            </td>
            <td>{$l['order']['day']}</td>
            <td>{$l['order']['name']}</td>
            <td>{$l['goods']['name']}</td>
            <td>{$l['gen_pay']}</td>
            <td>{$l['charge']}</td>
            <td>
                {$l['had_gen_pay']}
            </td>
            <td>
                {if $l['had_se_pay'] > 0}
                {$l['had_charge']}
                {else}
                <input type="text" id="had_charge{$l['id']}" value="{if $l['had_charge'] > 0}{$l['had_charge']}{else}{$l['charge']}{/if}" class="form-control" style="width:100px;" />
                {/if}
            </td>
            <td>
                {if $l['had_se_pay'] > 0}
                {$l['had_se_pay']}
                {else}
                <input type="text" id="had_se_pay{$l['id']}" value="{if $l['had_se_pay'] > 0}{$l['had_se_pay']}{else if $l['had_gen_pay'] > 0}{$l['had_gen_pay']}{else}{$l['gen_pay']}{/if}" class="form-control" style="color:red;width:100px;" />
                {/if}
            </td>
            <td>
                {if $l['had_se_pay'] > 0}
                已结算
                {else}
                未结算
                {/if}
            </td>
            <td>
                {if $l['had_se_pay'] > 0}
                {$l['se_remark']}
                {else}
                <input type="text" class="form-control se_remark" order_goods_id="{$l['id']}" id="se_remark{$l['id']}" value="{$l['se_remark']}" style="width: 100px;" />
                {/if}
            </td>
            <td>{$l['se_pay_day']}</td>
            <td>{$l['se_pay_user']}</td>
            <td>{$l['remark']}</td>
            <td>
                {if $l['had_se_pay'] > 0}
                {else}
                <button class="save_data btn btn-primary" order_id="{$l['id']}" id="save-btn{$l['id']}">结算</button>
                <button class="btn btn-danger cancel-data" id="cancel-btn{$l['id']}" order_id="{$l['id']}" style="display: none;">取消</button>
                {/if}
            </td>
        </tr>
        {/loop}
        <tr>
            <td colspan="4"></td>
            <td>合计:</td>
            <td>{$total_gen_pay}</td>
            <td>{$total_charge}</td>
            <td>{$total_had_gen_pay}</td>
            <td>{$total_had_charge}</td>
            <td>{$total_had_gen}</td>
            <td colspan="6"></td>
        </tr>
    </table>
    <div align="right">
        {if $type == 1}
        总手续费：<span id="total_charge">0</span>
        总结算金额：<span id="total_gen">0</span>
        应付金额：<span id="total_pay">0</span>
        <button class="btn btn-primary save-all">确定结算</button>
        {else if $type == 2}
        已收手续费：{$total_had_charge}
        已结算金额：{$total_had_gen}
        {/if}
    </div>
    <div>{$pager}</div>
</div>
{template 'common/footer'}